package com.huawei.hwid.c.a;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.text.TextUtils;
import com.huawei.hwid.c.b;
import com.huawei.hwid.core.d.b.e;
import com.huawei.hwid.core.datatype.HwAccount;
import com.huawei.hwid.core.encrypt.c;
import com.huawei.hwid.d;
import com.huawei.hwid.usecase.ClearDataUseCase;
import com.huawei.hwid.usecase.WriteHwAccountUseCase;
import com.huawei.hwid.usecase.WriteHwAccountUseCase.RequestValues;
import java.util.ArrayList;
import java.util.List;

public final class a implements b {
    private static a a;

    private a() {
    }

    public static synchronized a b(Context context) {
        a aVar;
        synchronized (a.class) {
            if (a == null) {
                e.a(context);
                a = new a();
                a.c(context);
            }
            aVar = a;
        }
        return aVar;
    }

    private void c(Context context) {
        e.b("SDKAccountManager", "init SDKAccountManager", true);
        c.a(context);
        com.huawei.hwid.core.d.a.c.a(context);
    }

    public boolean a(Context context, HwAccount hwAccount) {
        if (com.huawei.hwid.core.d.b.a(hwAccount)) {
            com.huawei.hwid.d.a.a(context).a(hwAccount);
            ArrayList arrayList = new ArrayList();
            arrayList.add(hwAccount);
            new com.huawei.hwid.b(d.a()).a(new WriteHwAccountUseCase(context, arrayList, true), new RequestValues(hwAccount.b()), null);
            return true;
        }
        e.b("SDKAccountManager", "the account is invalid , cannot be added into file", true);
        return false;
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public java.util.ArrayList<com.huawei.hwid.core.datatype.HwAccount> a(android.content.Context r6, java.lang.String r7) {
        /*
        r5 = this;
        r1 = new java.util.ArrayList;
        r1.<init>();
        r2 = r5.d(r6);
        monitor-enter(r2);
        r0 = android.text.TextUtils.isEmpty(r7);	 Catch:{ all -> 0x003b }
        if (r0 != 0) goto L_0x0016;
    L_0x0010:
        r0 = r2.isEmpty();	 Catch:{ all -> 0x003b }
        if (r0 == 0) goto L_0x0019;
    L_0x0016:
        monitor-exit(r2);	 Catch:{ all -> 0x003b }
        r0 = r1;
    L_0x0018:
        return r0;
    L_0x0019:
        r3 = r2.iterator();	 Catch:{ all -> 0x003b }
    L_0x001d:
        r0 = r3.hasNext();	 Catch:{ all -> 0x003b }
        if (r0 == 0) goto L_0x003e;
    L_0x0023:
        r0 = r3.next();	 Catch:{ all -> 0x003b }
        r0 = (com.huawei.hwid.core.datatype.HwAccount) r0;	 Catch:{ all -> 0x003b }
        if (r7 == 0) goto L_0x001d;
    L_0x002b:
        if (r0 == 0) goto L_0x001d;
    L_0x002d:
        r4 = r0.c();	 Catch:{ all -> 0x003b }
        r4 = r7.equals(r4);	 Catch:{ all -> 0x003b }
        if (r4 == 0) goto L_0x001d;
    L_0x0037:
        r1.add(r0);	 Catch:{ all -> 0x003b }
        goto L_0x001d;
    L_0x003b:
        r0 = move-exception;
        monitor-exit(r2);	 Catch:{ all -> 0x003b }
        throw r0;
    L_0x003e:
        monitor-exit(r2);	 Catch:{ all -> 0x003b }
        r0 = r1;
        goto L_0x0018;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.huawei.hwid.c.a.a.a(android.content.Context, java.lang.String):java.util.ArrayList<com.huawei.hwid.core.datatype.HwAccount>");
    }

    public ArrayList<HwAccount> a(Context context) {
        e.b("SDKAccountManager", "MemCache has no account,prase from file", true);
        ArrayList<HwAccount> a = b.a("accounts.xml", context, true);
        if (!a.isEmpty()) {
            com.huawei.hwid.d.a.a(context).a((HwAccount) a.get(0));
        }
        return a;
    }

    public boolean c(Context context, String str) {
        if (!com.huawei.hwid.core.d.b.h(context) || !com.huawei.hwid.core.d.b.j(context)) {
            return false;
        }
        Account[] accountsByType = AccountManager.get(context).getAccountsByType("com.huawei.hwid");
        if (accountsByType == null || accountsByType.length <= 0 || TextUtils.isEmpty(str)) {
            return false;
        }
        for (Account account : accountsByType) {
            if (account != null && str.equalsIgnoreCase(account.name)) {
                return true;
            }
        }
        return false;
    }

    public HwAccount a(Context context, String str, String str2) {
        List<HwAccount> d = d(context);
        if (d.isEmpty() || TextUtils.isEmpty(str)) {
            e.b("SDKAccountManager", "there has no account", true);
            return null;
        }
        synchronized (d) {
            for (HwAccount hwAccount : d) {
                if (a(str, hwAccount, str2)) {
                    return hwAccount;
                }
            }
            return null;
        }
    }

    private boolean a(String str, HwAccount hwAccount, String str2) {
        if (!(str == null || hwAccount == null || !str.equals(hwAccount.b()))) {
            if (TextUtils.isEmpty(str2)) {
                return true;
            }
            if (!TextUtils.isEmpty(str2) && str2.equals(hwAccount.c())) {
                return true;
            }
        }
        e.b("SDKAccountManager", "account inValid", true);
        return false;
    }

    public void b(Context context, String str) {
        com.huawei.hwid.d.a.a(context).a(null);
        com.huawei.hwid.d.a.a(context).c();
        new com.huawei.hwid.b(d.a()).a(new ClearDataUseCase(context), new ClearDataUseCase.RequestValues(), null);
    }

    private ArrayList<HwAccount> d(Context context) {
        ArrayList<HwAccount> arrayList = new ArrayList();
        HwAccount b = com.huawei.hwid.d.a.a(context).b();
        if (b == null) {
            e.b("SDKAccountManager", "MemCache has no account,prase from file", true);
            ArrayList<HwAccount> a = b.a("accounts.xml", context, true);
            if (a.isEmpty()) {
                return a;
            }
            com.huawei.hwid.d.a.a(context).a((HwAccount) a.get(0));
            return a;
        }
        e.b("SDKAccountManager", "use memcache account", true);
        arrayList.add(b);
        return arrayList;
    }
}
